package link.gpt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import link.gpt.dao.NewsCommentDao;
import link.gpt.dao.NewsDetailDao;
import link.gpt.pojo.NewsCommentExample;
import link.gpt.pojo.NewsDetail;
import link.gpt.service.NewsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version V1.0A
 * @uthor gpt
 * @Project holiday_task
 * @Package link.gpt.service.impl
 * @Description: ${TODO}
 * @Date 2021/10/7 15:47
 */
@Service
public class NewsDetailServiceImpl implements NewsDetailService {

   @Autowired
   private NewsDetailDao newsDetailDao;

   @Autowired
   private NewsCommentDao newsCommentDao;

   @Override
   public PageInfo<NewsDetail> findAllByTitleAndPage(String title, int num, int size) {
      PageHelper.startPage(num, size);
      List<NewsDetail> newsDetailList = newsDetailDao.findAllByTitleAndPage(title);
      return new PageInfo<>(newsDetailList);
   }

   @Override
   public int deleteNews(Integer newsId) {
      NewsCommentExample newsCommentExample = new NewsCommentExample();
      NewsCommentExample.Criteria criteria = newsCommentExample.createCriteria();
      criteria.andNewsIdEqualTo(newsId);
      newsCommentDao.deleteByExample(newsCommentExample);
      int count = newsDetailDao.deleteByPrimaryKey(newsId);
      return count;
   }
}
